package handler

import (
	"LogiTrans/cache"
	"LogiTrans/model"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func RepoAdd(c *gin.Context) {

	repoName := c.PostForm("repoName")
	repoContent := c.PostForm("repoContent")

	if repoName == "" || repoContent == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "不能为空",
		})
		return
	}

	data := model.Repo{
		RepoName:    repoName,
		RepoContent: repoContent,
	}

	model.DB.Create(&data)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}

func OrderAdd(c *gin.Context) {

	orderName := c.PostForm("orderName")
	orderContent := c.PostForm("orderContent")
	orderPrice, _ := strconv.Atoi(c.PostForm("orderPrice"))
	pid, _ := strconv.Atoi(c.PostForm("pid"))

	data := model.Orders{
		OrderName:    orderName,
		OrderContent: orderContent,
		OrderPrice:   orderPrice,
		Pid:          pid,
	}

	model.DB.Create(&data)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	if size > 50 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "页数<=50",
		})
		return
	}
	var order []model.OrdersRepo

	/*
				SELECT
		  orders.id AS ID,
		  orders.order_name AS OrderName,
		  orders.order_content AS OrderContent,
		  orders.order_price AS OrderPrice,
		  repos.repo_name AS RepoName,
		  repos.repo_content AS RepoContent
		FROM
		  repos
		  LEFT JOIN orders ON orders.pid = repos.id
	*/

	redis_key := "order_list_" + strconv.Itoa(page)
	redisData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()

	if redisData == "" {
		model.DB.Table("repos").
			Select(
				"orders.id AS ID",
				"orders.order_name AS OrderName",
				"orders.order_content AS OrderContent",
				"orders.order_price AS OrderPrice",
				"repos.repo_name AS RepoName",
				"repos.repo_content AS RepoContent",
			).
			Joins("LEFT JOIN orders ON orders.pid=repos.id").
			Offset(offset).
			Limit(size).
			Order("id asc").
			Find(&order)
		jsonData, _ := json.Marshal(order)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(redisData), &order)
	}

	var total int64
	model.DB.Model(&model.Orders{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "展示列表",
		"data":  order,
		"total": total,
	})
	return
}

func OrderRepoDetail(c *gin.Context) {
	id := c.Query("id")
	var order model.OrdersRepo

	/*
				SELECT
		  orders.id AS ID,
		  orders.order_name AS OrderName,
		  orders.order_content AS OrderContent,
		  orders.order_price AS OrderPrice,
		  repos.repo_name AS RepoName,
		  repos.repo_content AS RepoContent
		FROM
		  repos
		  LEFT JOIN orders ON orders.pid = repos.id
	*/

	redis_key := "order_detail_" + id
	redisData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()

	if redisData == "" {
		model.DB.Table("repos").
			Select(
				"orders.id AS ID",
				"orders.order_name AS OrderName",
				"orders.order_content AS OrderContent",
				"orders.order_price AS OrderPrice",
				"repos.repo_name AS RepoName",
				"repos.repo_content AS RepoContent",
			).
			Joins("LEFT JOIN orders ON orders.pid=repos.id").
			Where("orders.id=?", id).
			Find(&order)
		jsonData, _ := json.Marshal(order)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(redisData), &order)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "展示列表",
		"data": order,
	})
	return
}
